package belf.migrate.engine.ddl.helper.mysql;

import belf.migrate.api.exception.APIErrorCode;
import belf.migrate.api.exception.CatalogException;
import belf.migrate.engine.ddl.BaseDDLConvert;
import org.antlr.v4.runtime.tree.TerminalNode;

public class MySql2DMProcedureListener extends MySqlCommonListener {
    public MySql2DMProcedureListener(MySqlAntlrBuilder builder) {
        super(builder);
    }

    @Override
    public void visitTerminal(TerminalNode node) {
        String text = node.getText().trim();
        // mysql的select ... from ... into ..，, 需要改为达梦的：select ... into ... from ...
        //暂时以报错的方式返回给页面，提醒实施人员手动调整
        if ("INTO".equalsIgnoreCase(text) &&
                (BaseDDLConvert.hasAncestor("SelectIntoExpressionContext", node) ||
                        BaseDDLConvert.hasAncestor("SelectIntoVariablesContext", node)
                )) {
            builder.addError(new CatalogException(APIErrorCode.CONVERT_PROCEDURE_ERROR, "INTO...子句需要在SELECT ... FROM子句之间"));
        }
        if ("CONCAT".equalsIgnoreCase(text) && BaseDDLConvert.hasAncestor("FunctionNameBaseContext", node)) {
            builder.addError(new CatalogException(APIErrorCode.CONVERT_MYSQL_DDL_ERROR, "MySQL的CONCAT连接函数在达梦中需要两个及以上的参数！"));
        }
        builder.output(node);
    }
}
